﻿using Inaction.Mapping;
using System;
using System.Data;
using System.Data.Common;
using System.IO;

namespace Inaction.Util
{
    public static class Loger
    {
        static object flag = true;
        public static string LogDirectory
        {
            get { return AppHelper.LogDirectory; }
        }
        public static void WriteLog(string log)
        {
            WriteLog(log, LogType.Info);
        }

        internal static void WriteLog(SqlStatement sql)
        {
            if (sql == null) return;
            string content = sql.SqlText;
            if (sql.ParaValues.Count != 0)
            {
                content += Environment.NewLine;
                foreach (var para in sql.ParaValues)
                {
                    content += "[" + para.Name + ":" + para.Value + "]";
                }
            }
            WriteLog(content, LogType.Info);
        }

        public static void WriteLog(string log, LogType type = LogType.Info)
        {
            if (Inaction.Security.Principal.Instance == null)
            {
                WriteLog(log, type + "_System", type);
            }
            bool loged = Inaction.Security.Principal.Instance != null
            && Inaction.Security.Principal.Instance.Identity != null
            && !Inaction.Security.Principal.Instance.Identity.IsAuthenticated;

            string fileName = type + "_" + (loged ? Inaction.Security.Principal.Instance.Identity.UserNumber : "System");
            WriteLog(log, fileName, type);
        }

        public static void WriteLog(string log, string fileName, LogType type = LogType.Info)
        {
            var directory = System.IO.Path.Combine(LogDirectory, string.Format("{0:yyyyMMdd}", DateTime.Now));
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            string filePath = System.IO.Path.Combine(directory, fileName + ".txt");
            StreamWriter sw = null;
            try
            {
                lock (flag)
                {
                    if (!File.Exists(filePath))
                    {
                        sw = File.CreateText(filePath);
                    }
                    else
                    {
                        sw = File.AppendText(filePath);
                    }
                    sw.WriteLine(DateTime.Now);
                    sw.WriteLine(log);
                    sw.WriteLine();
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                if (sw != null) sw.Close();
            }
        }
       
        public static void WriteLog(Exception ex)
        {
            WriteLog(ex.GetMessage(true), LogType.Exception);
        }
        public static void DeleteHistoryLog(DateTime date)
        {
            string filePath = LogDirectory;
            var files = System.IO.Directory.GetFiles(filePath, "*.txt");
            foreach (var file in files)
            {
                if (File.GetCreationTime(file) < date)
                {
                    File.Delete(file);
                }
            }
        }
    }
    public enum LogType
    {
        Info,
        Exception,
        Error,
        Waring,
        Sql
    }
}
